<?php
//by willson 105328468@qq.com 2014/10/13
//member module interface

class coupon extends core {
	public $coupon_mod;
	public function __construct() {
		parent::__construct();
		$this->coupon_mod = m("coupon");
	}

	//我的优惠券列表
	//-2：过期，-1：已使用，1：正常
	public function index() {
		$this->_set_expired();
		$where = " cs.member_id='{$this->member_id}' AND cs.member_id>'0'";
		$status = isset($_GET['status']) ? $_GET['status'] : '';
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		if ($status != '') {
			//失效：包括过期和已使用的
			$status = intval($status);
			if ($status==0) {
				$where .= " AND cs.status<='0'";
			} else {
				$where .= " AND cs.status='{$status}'";
			}
		}
		$sql = "SELECT cs.*, c.name, c.begin_time, c.end_time, c.min_price, c.permit_goods 
			FROM jf_coupon_sn cs INNER JOIN jf_coupon c ON cs.coupon_id=c.id 
			WHERE {$where} ORDER BY cs.id DESC LIMIT {$offset},{$pagesize}";
		//exit($sql);
		$coupons = $this->db->get_results($sql);
		if ($coupons) {
			foreach ($coupons as $k=>$coupon) {
				$coupons[$k]->begin_time = date("Y-m-d", $coupon->begin_time);
				$coupons[$k]->end_time = date("Y-m-d", $coupon->end_time);
				$coupons[$k]->coupon_money = intval($coupon->coupon_money);
				if (intval($coupon->min_price)) {
					$min_price = intval($coupon->min_price);
					$coupons[$k]->min_price_memo = "满{$min_price}元可用";
				} else {
					$coupons[$k]->min_price_memo = "无门槛";
				}
				if ($coupon->permit_goods) {
					$coupons[$k]->memo = "（部分商品可用）";
				} else {
					$coupons[$k]->memo = "（全场通用）";
				}
			}
		}
		success($coupons);
	}

	//优惠券详情，可用在html5页面
	public function detail() {
		$coupon_id = isset($_GET['coupon_id']) ? intval($_GET['coupon_id']) : 0;
		$sql = "SELECT *, '' as goods FROM jf_coupon WHERE id='{$coupon_id}'";
		$coupon = $this->db->get_row($sql);
		if ($coupon) {
			if ($coupon->permit_goods==1) {
				$goods_ids = $this->coupon_mod->get_coupon_goods($coupon_id);
				if ($goods_ids) {
					$goods_ids = implode(",", $goods_ids);
					$sql = "SELECT id, name FROM jf_goods WHERE id IN ($goods_ids)";
					$goods = $this->db->get_results($sql);
					$coupon->goods = $goods;
				}
			}
		}
		success($coupon);
	}


	//领取优惠券
	public function ling() {
		if (!$this->member_id) error('请登录', -100);
		$coupon_id = isset($_GET['coupon_id']) ? intval($_GET['coupon_id']) : 0;
		$sql = "SELECT * FROM jf_coupon WHERE id='{$coupon_id}'";
		$coupon = $this->db->get_row($sql);
		if ($coupon) {
			$num_per_person = $coupon->num_per_person;
			if ($num_per_person) {
				$sql = "SELECT COUNT(*) FROM jf_coupon_sn WHERE member_id='{$this->member_id}' AND coupon_id='{$coupon_id}'";
				$num = intval($this->db->get_var($sql));
				if ($num>=$num_per_person) {
					error('你已经领取了该优惠券');
				} else {
					if ($this->_ling($coupon_id)) {
						success("ok");
					}
				}
			//优惠券没有限制领取数量
			} else {
				if ($this->_ling($coupon_id)) {
					success("ok");
				}
			}
		}
		error('领取优惠券失败');
	}

	//领取优惠券，先判断优惠券是否有效
	private function _ling($coupon_id) {
		$sql = "SELECT min(cs.id) FROM jf_coupon_sn cs INNER JOIN jf_coupon c ON cs.coupon_id=c.id 
			WHERE coupon_id='{$coupon_id}' AND cs.status='1' AND c.status='1' AND cs.member_id='0'";
		$coupon_sn_id = intval($this->db->get_var($sql));
		if ($coupon_sn_id) {
			$sql = "UPDATE jf_coupon_sn SET member_id='{$this->member_id}', get_time='".time()."' WHERE id='{$coupon_sn_id}'";
			//echo $sql;
			$ok = $this->db->query($sql);
			if ($ok) {
				return true;
			}
		}
		return false;
	}


	//手工添加优惠券
	public function add() {
		$sn = isset($_POST['sn']) ? trim($_POST['sn']) : '';
		if ($sn) {
			$sql = "SELECT id FROM jf_coupon_sn WHERE sn='{$sn}' AND member_id='0'";
			$id = intval($this->db->get_var($sql));
			if ($id) {
				$sql = "UPDATE jf_coupon_sn SET member_id='{$this->member_id}', get_time='".time()."' WHERE id='{$id}'";
				$this->db->query($sql);
				success("ok");
			} else {
				error("优惠券码不正确");
			}
		}
	}

	//设置过期的优惠券
	private function _set_expired() {
		$now = time();
		$sql = "SELECT cs.id, c.name, c.begin_time, c.end_time  
			FROM jf_coupon_sn cs INNER JOIN jf_coupon c ON cs.coupon_id=c.id 
			WHERE cs.member_id='{$this->member_id}' AND c.end_time<'{$now}'";
		$coupons = $this->db->get_results($sql);
		if ($coupons) {
			$ids = "";
			foreach ($coupons as $key => $coupon) {
				if ($ids) {
					$ids .= ", {$coupon->id}";
				} else {
					$ids = "{$coupon->id}";
				}
			}
			if ($ids) {
				$sql = "UPDATE jf_coupon_sn SET status='-2' WHERE member_id='{$this->member_id}' AND id IN ($ids)";
				$this->db->query($sql);
			}
		}		
	}
}